package com.gec.teams.wechat;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.gec.teams.wechat.config.SystemConstants;
import com.gec.teams.wechat.mapper.SysConfigMapper;
import com.gec.teams.wechat.pojo.SysConfig;
import com.gec.teams.wechat.util.SysDateTime;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

import javax.annotation.PostConstruct;
import java.lang.reflect.Field;
import java.util.List;

@SpringBootApplication
@Slf4j
@ServletComponentScan
@MapperScan("com.gec.teams.wechat.mapper")
@EnableScheduling
//启动异步多线程
@EnableAsync
public class TeamsWechatApiApplication {
    public static void main(String[] args) {
        SpringApplication.run(TeamsWechatApiApplication.class, args);

    }

    @Autowired
    private SysConfigMapper sysConfigMapper;

    @Autowired
    private SystemConstants constants;

//    @Autowired
//    private SysDateTime sysDateTime;
    @PostConstruct
    public void init(){
        List<SysConfig> sysConfigs = sysConfigMapper.getAllSysConfig();
            sysConfigs.forEach(one->{
                String paramKey = one.getParamKey();
                //下划线转换大小写
                paramKey = StrUtil.toCamelCase(paramKey);
                String today = DateUtil.today();
                String value = today+" "+ one.getParamValue();
                try{
                    //利用反射，来把sysConfig里面的值，反射到SystemConstants的属性名中
                    Field filed = constants.getClass().getDeclaredField(paramKey);//获取config中的属性名
                    filed.set(constants,value);//把对应属性名里对应的值放进去
                }catch (Exception e){
                    log.error("配置初始化执行异常");
                }
            });
        System.out.println(constants);
    }

}
